/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package submarines;

import java.util.List;
import SubmarinesBindings.*;
import javax.swing.text.Position;
import javax.xml.bind.*;
/**
 *
 * @author sshiran
 */
public class ownBoard extends Board {
    private Player playerBoard;    
    
    public ownBoard(Player playerBoard ){
        super();
        this.playerBoard = playerBoard;
    }
    
    public ownBoard(SubmarinesBindings.Board b, List<SubmarinesBindings.Boat> xmlBoats, Player boardPlayer) throws JAXBException {
        super();
        SubmarinesBindings.Boat boat;
        int i;
        List<Location> postions;
        Location start;
        String coords;
        postions = b.getLocation();
        int dir, row, col, shipSize;
        this.playerBoard = boardPlayer;
        
        for (i = 0; i < xmlBoats.size(); i++) {//create boats from XML file
            boat = xmlBoats.get(i);
            shipSize = (int)boat.getSize();
            if ("HORIZONTAL".equals(postions.get(i).getDirection())) {
                dir = 1;
                start = postions.get(i);
                coords = start.getPosition();
                col = coords.charAt(0) - 'A' + Config.BOARD_START;
                row = coords.charAt(1) - '1' + Config.BOARD_START;
                if ((row < Config.BOARD_START) || (row > Config.BOARD_SIZE) || (col < Config.BOARD_START) || (col > Config.BOARD_SIZE - shipSize)) {
                    throw new JAXBException("Coordinates " + coords + " are invalid for horizontal ship at size " + shipSize + "");
                }
                boolean flag = insertShip(row, col, shipSize, dir);
                if (!flag) {
                    throw new JAXBException("Problem adding ship");
                }
            } else if ("VERTICAL".equals(postions.get(i).getDirection())) {
                dir = 2;
                start = postions.get(i);
                coords = start.getPosition();
                col = coords.charAt(0) - 'A' + Config.BOARD_START;
                row = coords.charAt(1) - '1' + Config.BOARD_START;
                if ((row < Config.BOARD_START) || (row > Config.BOARD_SIZE - shipSize) || (col < Config.BOARD_START) || (col > Config.BOARD_SIZE)) {
                    throw new JAXBException("Coordinates " + coords + " are invalid for vertival ship at size " + shipSize + "");
                }
                boolean flag = insertShip(row, col, shipSize, dir);
                if (!flag) {
                    throw new JAXBException("Problem adding ship");
                }
            } else {
                throw new JAXBException("Invalid direction");
            }
        }
    }

    @Override
    public Player getPlayerOwner() {
        return playerBoard;
    }
}
